<?php defined('SMIT_PATH') or die('Access Denied');
/**
 *	SmitDebug.php - Debugging class
 *	This is Smit Matterhorn - Build 24109
 *	
 *
 * @author		The SmitBoard Team & Community
 * @license		GNU GPL v3
 * @link		http://smitboard.sf.net
 * @since		Matterhorn
 */
 
class SmitDebug
{
	public $base;
	
	private static $instance;
	
	public $debug = array();

	public $flushed = FALSE;
	
	public $exec = array();

	private function __construct()
	{
		$this->base = SmitBase::getInstance();
		$this->exec = array('timers' => array(), 'memory' => array());
	}

    public function __clone()
    {
        trigger_error('cloning denied.', E_USER_ERROR);
    }

	static function getInstance()
	{
		if(!is_object(self::$instance))
		{
			self::$instance = new self;
		}
		return self::$instance;
	}
	
	public function send($message, $type = DEBUG_GENERAL)
	{
		switch($type)
		{
			default:
			case DEBUG_GENERAL:
				$type = 'General';
			break;
			case DEBUG_SQL:
				$type = 'SQL';
			break;
			case DEBUG_QUERY:
				$query = end($this->base->db->queries);
				$type = 'Query';
				$message = str_replace(array(
				'SELECT ',
				'UPDATE ',
				' SET ',
				' FROM ',
				' WHERE'
				),
				array(
				'<span style="color: green">SELECT</span> ', 
				'<span style="color: green">UPDATE</span> ', 
				' <span style="color: red">SET</span> ',
				' <span style="color: red">FROM</span> ',
				' <span style="color: red">WHERE</span>'
				),
				$message) . '<br />[Rows: ' . (substr($query['query'], 0, 6) == 'SELECT' ? $this->base->db->num_rows($query['result']) : $this->base->db->affected_rows($query['result'])) . '][Execution Time: ' . $query['time'] . ']';
			break;
			case DEBUG_NOTICE:
				$type = 'NOTICE';
			break;
		}
		$this->debug[] = '<div class="bottom"><span class="title" style="color:blue">[ ' . $type . ' ]</span> <p class="info">' . $message . '</p></div>';
	}
	
	public function general($message)
	{
		$this->send($message, DEBUG_GENERAL);
	}

	public function notice($message)
	{
		$this->send($message, DEBUG_NOTICE);
	}

	public function sql($message)
	{
		$this->send($message, DEBUG_SQL);
	}

	public function query($message)
	{
		$this->send($message, DEBUG_QUERY);
	}

	public function call()
	{
		if(defined('DEBUG_ON') && DEBUG_ON == 1 && !$this->flushed)
		{
			$this->flushed = TRUE;
			/*
			$sql = '';
			$sql .= '<div class="bottom">Using the <span class="b">' . $this->base->db->driver . ' ' .  $this->base->db->driver_version . '</span> Driver, Though PHP\'s <span class="b">' . $this->base->db->dependency . '</span> Extension</div>';
			foreach($this->base->db->queries as $query)
			{
				$sql .= '<div class="bottom">' . 
				str_replace(array('SELECT ', ' FROM ', ' WHERE'), array('<span style="color: blue">SELECT</span> ', ' <span style="color: green">FROM</span> ', ' <span style="color: green">WHERE</span>'), $query['query'])
				. '<br />[Rows: ' . $this->base->db->num_rows($query['result']) . '][Execution Time: ' . $query['time'] . ']</div>';
			}
			*/
			$out = str_replace('<%DEBUG%>', implode("\n\t\t\t", $this->debug), file_get_contents(SMIT_SYS_PATH . 'core/debug.html'));
			exit($out);
		}
	}

	public function timer($name)
	{
		if(!array_key_exists($name, $this->exec['timers']))
		{
			$this->exec['timers'][$name] = array();
		}
		if(array_key_exists($name, $this->exec['timers']))
		{
			if(isset($this->exec['timers'][$name]['start']))
			{
				$this->exec['timers'][$name]['end'] = microtime(TRUE);
				$this->exec['timers'][$name]['total'] = round($this->exec['timers'][$name]['end'] - $this->exec['timers'][$name]['start'], 2);
			}
			else
			{
				$this->exec['timers'][$name]['start'] = microtime(TRUE);
			}
			return $this->exec['timers'][$name];
		}
		return FALSE;
	}
	public function memory($name)
	{
		if(!array_key_exists($name, $this->exec['memory']))
		{
			$this->exec['memory'][$name] = array();
		}
		if(function_exists('memory_get_usage') && array_key_exists($name, $this->exec['memory']))
		{
			if(isset($this->exec['memory'][$name]['start']))
			{
				$this->exec['memory'][$name]['end'] = memory_get_usage();
				$this->exec['memory'][$name]['total'] = round(($this->exec['memory'][$name]['end'] - $this->exec['memory'][$name]['start']) / 1024 / 1024, 2);
			}
			else
			{
				$this->exec['memory'][$name]['start'] = memory_get_usage();
			}
			return $this->exec['memory'][$name];
		}
		return FALSE;
	}
}
?>